package com.willie.boot.rabbit.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;


/**
 * <p>
 * mq配置类
 * </p>
 *
 * @author willie
 * @since 2019/3/13
 */
@Configuration
public class RabbitmqConfig {
    private final String orderTtlQueue = "order.ttl";
    public static final String orderDelayQueue = "order.delay";
    public static final String orderDelayRoute = "order.delay";
    public static final String orderFanoutExchange = "orderFanoutExchange";
    public static final String orderDelayExchange = "orderDelayExchange";

    @Bean
    public Queue orderTtlQueue() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-message-ttl", 50 * 1000);
        args.put("x-dead-letter-exchange", orderDelayExchange);
        args.put("x-dead-letter-routing-key", orderDelayRoute);
        return new Queue(orderTtlQueue, true, false, false, args);
    }

    @Bean
    public Queue orderDelayQueue() {
        return new Queue(orderDelayQueue);
    }

    @Bean(name = "orderFanoutExchange")
    public FanoutExchange orderFanoutExchange() {
        return new FanoutExchange(orderFanoutExchange);
    }

    @Bean(name = "orderDelayExchange")
    public FanoutExchange orderDelayExchange() {
        return new FanoutExchange(orderDelayExchange);
    }

    @Bean
    public Binding bindOrderTtlQueueToOrderFanoutExchange(Queue orderTtlQueue, FanoutExchange orderFanoutExchange) {
        return BindingBuilder.bind(orderTtlQueue).to(orderFanoutExchange);
    }

    @Bean
    public Binding bindOrderDelayQueueToOrderDalayExchange(Queue orderDelayQueue, FanoutExchange orderDelayExchange) {
        return BindingBuilder.bind(orderDelayQueue).to(orderDelayExchange);
    }
}
